package com.xxg.renrenorder.message;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.xxg.renrenorder.component.UserBenefitComponent;
import com.xxg.renrenorder.dao.bean.OrderMsg;
import com.xxg.renrenorder.service.OrderService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

import static com.xxg.renrenorder.config.RabbitMQConfig.*;


/**
 * 将超时支付的订单关闭
 *
 * @author xxg
 * @date 2020/12/29
 */
@Component
public class OrderMessageConsumer {
    private static final Logger logger = LoggerFactory.getLogger(OrderMessageConsumer.class);
    private final OrderService orderService;
    private final UserBenefitComponent userBenefitComponent;

    public OrderMessageConsumer(OrderService orderService, UserBenefitComponent userBenefitComponent) {
        this.orderService = orderService;
        this.userBenefitComponent = userBenefitComponent;
    }

    @RabbitListener(queues = DEAD_LETTER_QUEUE_NAME)
    public void receive(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        logger.info("收到订单消息：" + msg);
        OrderMsg orderMsg = JSON.parseObject(msg, OrderMsg.class);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        // 如果订单超时就取消订单
        boolean flag = orderService.cancelOrderById(orderMsg.getId());
        if (flag) {
            logger.info("取消超时订单，订单ID:" + orderMsg.getId());
        }
    }


    /**
     * 10天自动收货
     */
    @RabbitListener(queues = DEAD_LETTER_QUEUEA_NAME)
    public void receiveA(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        logger.info("A收到订单消息：" + msg);
        OrderMsg orderMsg = JSON.parseObject(msg, OrderMsg.class);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        // 如果订单发货10天后未确认则自动确认收货
        boolean flag = orderService.autoCommitOrder(orderMsg.getId());
        if (flag) {
            logger.info("订单超过10天自动收货，订单ID:" + orderMsg.getId());
        }
    }

    /**
     * 15天售后关闭
     */
    @RabbitListener(queues = DEAD_LETTER_QUEUEB_NAME)
    public void receiveB(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        logger.info("B收到订单消息：" + msg);
        OrderMsg orderMsg = JSON.parseObject(msg, OrderMsg.class);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        // 如果订单超时就取消订单
        boolean flag = orderService.autoDisableOrderAfterService(orderMsg.getId());
        if (flag) {
            logger.info("订单确认收货后超过15天关闭售后，订单ID:" + orderMsg.getId());
        }
    }

    /**
     * 15天收益结算
     */
    @RabbitListener(queues = DEAD_LETTER_QUEUEC_NAME)
    public void receiveC(Message message, Channel channel) throws IOException {
        String msg = new String(message.getBody());
        logger.info("C收到订单消息：" + msg);
        OrderMsg orderMsg = JSON.parseObject(msg, OrderMsg.class);
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        // 15天收益结算
        boolean flag = userBenefitComponent.autoCommitBenefitStatus(orderMsg.getId());
        if (flag) {
            logger.info("订单确认收货后超过15天后收益结算，订单ID:" + orderMsg.getId());
        }
    }

}
